package com.zbkj.crmeb.store.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.zbkj.crmeb.store.dao.HomeActivityDao;
import com.zbkj.crmeb.store.dto.HomeActivityDTO;
import com.zbkj.crmeb.store.model.HomeActivity;
import com.zbkj.crmeb.store.request.HomeActivityRequest;
import com.zbkj.crmeb.store.service.HomeActivityService;
import com.zbkj.crmeb.store.service.HomeStyleService;

@Service
public class HomeActivityImpl extends ServiceImpl<HomeActivityDao, HomeActivity> implements HomeActivityService {
    @Resource
    private HomeActivityDao homeActivityDao;
    @Resource
    private HomeStyleService homeStyleService;

    @Override
    public List<HomeActivityDTO> listEntity(Integer merId) {
        List<HomeActivity> homeActivitys = this.list(Wrappers.<HomeActivity>lambdaQuery()
                .eq(HomeActivity :: getMerId, merId)
                );
        // 对象复制
        List<HomeActivityDTO> homeActivityDTO = new ArrayList<>();
        for(HomeActivity homeActivity : homeActivitys) {
            HomeActivityDTO dto = new HomeActivityDTO();
            BeanUtils.copyProperties(homeActivity, dto);
            dto.setHomeStryles(homeStyleService.listEntityByActivityId(homeActivity.getId()));
            homeActivityDTO.add(dto);
        }

        return homeActivityDTO;
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<HomeActivityDTO> pageEntity(Integer merId, HomeActivityRequest request) {
        Page<Object> page = PageHelper.startPage(request.getPage(), request.getPageSize());
        List<HomeActivity> homeActivitys = homeActivityDao.selectList(Wrappers.<HomeActivity>lambdaQuery()
        		.like(StringUtil.isNotEmpty(request.getName()), HomeActivity :: getName, request.getName())
                .eq(HomeActivity :: getMerId, merId)
                .ne(request.getId()!=null,HomeActivity::getId,request.getId())
                .orderByDesc(HomeActivity::getCreateTime)
        		);

        // 对象复制
        List<HomeActivityDTO> homeActivityDTO = new ArrayList<>();
        for(HomeActivity homeActivity : homeActivitys) {
            HomeActivityDTO dto = new HomeActivityDTO();
            BeanUtils.copyProperties(homeActivity, dto);
            dto.setHomeStryles(homeStyleService.listEntityByActivityId(homeActivity.getId()));
            homeActivityDTO.add(dto);
        }

        return CommonPage.copyPageInfo(page, homeActivityDTO);
    }

    @Override
    public Boolean saveEntity(Integer merId, List<HomeActivityDTO> homeActivityDTOs) {
        List<HomeActivity> homeActivitys = new ArrayList<>();
        for(int i=0; i<homeActivityDTOs.size(); i++) {
            // 对象赋值
            homeActivityDTOs.get(i).setMerId(merId);
            HomeActivity homeActivity = new HomeActivity();
            BeanUtils.copyProperties(homeActivityDTOs.get(i), homeActivity);
            if(null == homeActivity.getId()) {
                homeActivity.setCreateTime(new Date());
            } else {
                HomeActivity homeActivityDB = this.getOne(Wrappers.<HomeActivity>lambdaQuery()
                        .eq(HomeActivity :: getMerId, merId)
                        .eq(HomeActivity :: getId, homeActivityDTOs.get(i).getId())
                        .last("LIMIT 1")
                        );
                if(null == homeActivityDB) {
                    throw new CrmebException("记录不存在");
                }
            }
            homeActivity.setUpdateTime(new Date());
            homeActivitys.add(homeActivity);
        }

        return this.saveOrUpdateBatch(homeActivitys);
    }

}
